package mj.scott.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mj.scott.mapper.ShoppingCartMapper;
import mj.scott.pojo.entity.ShoppingCart;
import mj.scott.service.IShoppingCartService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    /**
     * 添加商品到购物车：同一桌号+菜品+口味的商品数量累加
     */
    @Override
    @Transactional
    public ShoppingCart add(ShoppingCart shoppingCart) {
        // 构建查询条件：桌号+菜品+口味（唯一标识同一商品）
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getTableId, shoppingCart.getTableId())
                .eq(ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());

        ShoppingCart existCart = this.getOne(queryWrapper);
        if (existCart != null) {
            // 已存在则数量+1
            existCart.setNumber(existCart.getNumber() + 1);
            this.updateById(existCart);
            return existCart;
        } else {
            // 不存在则新增
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
            return shoppingCart;
        }
    }

    /**
     * 减少购物车商品数量：数量-1，若数量=0则删除
     */
    @Override
    @Transactional
    public ShoppingCart sub(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getTableId, shoppingCart.getTableId())
                .eq(ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());

        ShoppingCart existCart = this.getOne(queryWrapper);
        if (existCart == null) {
            throw new RuntimeException("购物车中不存在该商品");
        }

        // 数量-1
        int newNumber = existCart.getNumber() - 1;
        if (newNumber <= 0) {
            // 数量为0则删除
            this.removeById(existCart.getId());
            return null;
        } else {
            existCart.setNumber(newNumber);
            this.updateById(existCart);
            return existCart;
        }
    }

    /**
     * 查询指定桌号的购物车列表
     */
    @Override
    public List<ShoppingCart> getByTableId(Long tableId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getTableId, tableId)
                .orderByAsc(ShoppingCart::getCreateTime); // 按添加时间排序
        return this.list(queryWrapper);
    }

    /**
     * 清空指定桌号的购物车
     */
    @Override
    @Transactional
    public void cleanByTableId(Long tableId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getTableId, tableId);
        this.remove(queryWrapper);
    }
}
